#### Figure 2 Data (Unconditional AMCE) ####
fig2_dat = table_conjoint(conjoint = dta_grp |>
                            select(-ends_with("_grp")),
                          lab = policies,
                          weights = dta_grp$demog_weight,
                          use="all",
                          grouped_data=FALSE)
export(fig2_dat, here("data","fig2_dat.rds"))

#### Figure 3 Data (AMCE by inc/edu) ####
fig3_inc_dat = table_conjoint(conjoint = dta_grp |>
                                mutate(group = inc_grp) |>
                                filter(!is.na(group)) |>
                                select(-ends_with("_grp")),
                              lab = policies,
                              weights = dta_grp$demog_weight[!is.na(dta_grp$inc_grp)],
                              use="all",
                              grouped_data=T)
export(fig3_inc_dat, here("data","fig3_inc_dat.rds"))

fig3_edu_dat = table_conjoint(conjoint = dta_grp |>
                                mutate(group = edu_grp) |>
                                filter(!is.na(group)) |>
                                select(-ends_with("_grp")),
                              lab = policies,
                              weights = dta_grp$demog_weight[!is.na(dta_grp$edu_grp)],
                              use="all",
                              grouped_data=T)
export(fig3_edu_dat, here("data","fig3_edu_dat.rds"))

## Appendix: AMCE w/ Very High Income

fig3_inc2_dat = table_conjoint(conjoint = dta_grp |>
                                mutate(group = inc2_grp) |>
                                filter(!is.na(group)) |>
                                select(-ends_with("_grp")),
                              lab = policies,
                              weights = dta_grp$demog_weight[!is.na(dta_grp$inc2_grp)],
                              use="all",
                              grouped_data=T)
export(fig3_inc2_dat, here("data","fig3_inc2_dat.rds"))

#### Figure 4 Data (inc conditional on agreement) ####
fig4_inc_agree_dat = table_conjoint(
  conjoint = dta_grp |>
    mutate(group = inc_grp) |>
    filter(!is.na(group)) |>
    select(-ends_with("_grp")),
  lab = policies,
  weights = dta_grp$demog_weight[!is.na(dta_grp$inc_grp)],
  use="for",
  grouped_data=T
)
export(fig4_inc_agree_dat, here("data","fig4_inc_agree_dat.rds"))

fig4_inc_disagree_dat = table_conjoint(
  conjoint = dta_grp |>
    mutate(group = inc_grp) |>
    filter(!is.na(group)) |>
    select(-ends_with("_grp")),
  lab = policies,
  weights = dta_grp$demog_weight[!is.na(dta_grp$inc_grp)],
  use="against",
  grouped_data=T
)
export(fig4_inc_disagree_dat, here("data","fig4_inc_disagree_dat.rds"))

## Appendix Data: Alternative Income Split (Very High Income)
fig4_inc2_agree_dat = table_conjoint(
  conjoint = dta_grp |>
    mutate(group = inc2_grp) |>
    filter(!is.na(group)) |>
    select(-ends_with("_grp")),
  lab = policies,
  weights = dta_grp$demog_weight[!is.na(dta_grp$inc2_grp)],
  use="for",
  grouped_data=T
)
export(fig4_inc2_agree_dat, here("data","fig4_inc2_agree_dat.rds"))

fig4_inc2_disagree_dat = table_conjoint(
  conjoint = dta_grp |>
    mutate(group = inc2_grp) |>
    filter(!is.na(group)) |>
    select(-ends_with("_grp")),
  lab = policies,
  weights = dta_grp$demog_weight[!is.na(dta_grp$inc2_grp)],
  use="against",
  grouped_data=T
)
export(fig4_inc2_disagree_dat, here("data","fig4_inc_disagree2_dat.rds"))


## Appendix Data: Low-Income, by Education Level ##
fig4a_inc_agree_dat = table_conjoint(
  conjoint = dta_grp |>
    mutate(group = edu_inc_grp) |>
    filter(!is.na(group)) |>
    select(-ends_with("_grp")),
  lab = policies,
  weights = dta_grp$demog_weight[!is.na(dta_grp$edu_inc_grp)],
  use="for",
  grouped_data=T
)

export(fig4a_inc_agree_dat, here("data","fig4a_inc_agree_dat.rds"))

fig4a_inc_disagree_dat = table_conjoint(
  conjoint = dta_grp |>
    mutate(group = edu_inc_grp) |>
    filter(!is.na(group)) |>
    select(-ends_with("_grp")),
  lab = policies,
  weights = dta_grp$demog_weight[!is.na(dta_grp$edu_inc_grp)],
  use="against",
  grouped_data=T
)

export(fig4a_inc_disagree_dat, here("data","fig4a_inc_disagree_dat.rds"))

#### Figure 6 Data (edu conditional on agreement) ####
fig6_edu_agree_dat = table_conjoint(
  conjoint = dta_grp |>
    mutate(group = edu_grp) |>
    filter(!is.na(group)) |>
    select(-ends_with("_grp")),
  lab = policies,
  weights = dta_grp$demog_weight[!is.na(dta_grp$edu_grp)],
  use="for",
  grouped_data=T
)
export(fig6_edu_agree_dat, here("data","fig6_edu_agree_dat.rds"))

fig6_edu_disagree_dat = table_conjoint(
  conjoint = dta_grp |>
    mutate(group = edu_grp) |>
    filter(!is.na(group)) |>
    select(-ends_with("_grp")),
  lab = policies,
  weights = dta_grp$demog_weight[!is.na(dta_grp$edu_grp)],
  use="against",
  grouped_data=T
)
export(fig6_edu_disagree_dat, here("data","fig6_edu_disagree_dat.rds"))

#### Figure 8 Data (Republican edu conditional on agreement) ####
dta_rep = dta_grp |> 
  filter(str_detect(demog_pid7, "Republican"))

fig8_r_edu_agree_dat = table_conjoint(
  conjoint = dta_rep |>
    filter(str_detect(demog_pid7, "Republican")) |> 
    mutate(group = edu_grp) |>
    filter(!is.na(group)) |>
    select(-ends_with("_grp")),
  lab = policies,
  weights = dta_rep$demog_weight[!is.na(dta_rep$edu_grp)],
  use="for",
  grouped_data=T
)
export(fig8_r_edu_agree_dat, here("data","fig8_r_edu_agree_dat.rds"))

fig8_r_edu_disagree_dat = table_conjoint(
  conjoint = dta_rep |>
    filter(str_detect(demog_pid7, "Republican")) |> 
    mutate(group = edu_grp) |>
    filter(!is.na(group)) |>
    select(-ends_with("_grp")),
  lab = policies,
  weights = dta_rep$demog_weight[!is.na(dta_rep$edu_grp)],
  use="against",
  grouped_data=T
)
export(fig8_r_edu_disagree_dat, here("data","fig8_r_edu_disagree_dat.rds"))

rm(dta_rep)

#### Appendix Data ####

## Gender ##

fig_sex_agree_dat = table_conjoint(
  conjoint = dta_grp |>
    mutate(group = male_grp) |>
    filter(!is.na(group)) |>
    select(-ends_with("_grp")),
  lab = policies,
  weights = dta_grp$demog_weight[!is.na(dta_grp$male_grp)],
  use="for",
  grouped_data=T
)
export(fig_sex_agree_dat, here("data","fig_sex_agree_dat.rds"))

fig_sex_disagree_dat = table_conjoint(
  conjoint = dta_grp |>
    mutate(group = male_grp) |>
    filter(!is.na(group)) |>
    select(-ends_with("_grp")),
  lab = policies,
  weights = dta_grp$demog_weight[!is.na(dta_grp$male_grp)],
  use="against",
  grouped_data=T
)
export(fig_sex_disagree_dat, here("data","fig_sex_disagree_dat.rds"))

## Race/Ethnicity ##

fig_race_agree_dat = table_conjoint(
  conjoint = dta_grp |>
    mutate(group = white_grp) |>
    filter(!is.na(group)) |>
    select(-ends_with("_grp")),
  lab = policies,
  weights = dta_grp$demog_weight[!is.na(dta_grp$white_grp)],
  use="for",
  grouped_data=T
)
export(fig_race_agree_dat, here("data","fig_race_agree_dat.rds"))

fig_race_disagree_dat = table_conjoint(
  conjoint = dta_grp |>
    mutate(group = white_grp) |>
    filter(!is.na(group)) |>
    select(-ends_with("_grp")),
  lab = policies,
  weights = dta_grp$demog_weight[!is.na(dta_grp$white_grp)],
  use="against",
  grouped_data=T
)
export(fig_race_disagree_dat, here("data","fig_race_disagree_dat.rds"))

## PID ##

fig_pid_agree_dat = table_conjoint(
  conjoint = dta_grp |>
    mutate(group = dem_grp) |>
    filter(!is.na(group)) |>
    select(-ends_with("_grp")),
  lab = policies,
  weights = dta_grp$demog_weight[!is.na(dta_grp$dem_grp)],
  use="for",
  grouped_data=T
)
export(fig_pid_agree_dat, here("data","fig_pid_agree_dat.rds"))

fig_pid_disagree_dat = table_conjoint(
  conjoint = dta_grp |>
    mutate(group = dem_grp) |>
    filter(!is.na(group)) |>
    select(-ends_with("_grp")),
  lab = policies,
  weights = dta_grp$demog_weight[!is.na(dta_grp$dem_grp)],
  use="against",
  grouped_data=T
)
export(fig_pid_disagree_dat, here("data","fig_pid_disagree_dat.rds"))


#### Calculate Difference in Means AMCE ####

exclude_set = c(
  colnames(dta_grp)[str_detect(colnames(dta_grp), "demog_")],
  colnames(dta_grp)[str_detect(colnames(dta_grp), "policy_")],
  colnames(dta_grp)[str_detect(colnames(dta_grp), "_pres")],
  colnames(dta_grp)[str_detect(colnames(dta_grp), "_local")]
)

which_effects_dta_grp =
  setdiff(
    colnames(dta_grp),
    # Which columns actually have dta_grp data to fill in?
    c("choice", "person_id", "question", "set","ResponseId",
      "user_agent","user_agent_type","user_agent_ver","user_agent_os","user_agent_os_ver",
      "week","group","wave","race_pres","race_local", exclude_set))

# Get names of the associated policy questions, and compute agreement scores
policies_in <- paste("policy_",which_effects_dta_grp,sep="")

# Subset to items where we have the associated policy question, unless use="all"
which_effects_dta_grp = which_effects_dta_grp[policies_in %in% colnames(dta_grp)]
policies_in = policies_in[policies_in %in% colnames(dta_grp)]

# If policy settings are equal, replace with NA
dta_grp_int = dta_grp |> 
  group_by(person_id, question) |> 
  mutate(across(all_of(which_effects_dta_grp),
                ~ replace(.x, n_distinct(.x) == 1, NA))) |> 
  ungroup()

write_parquet(dta_grp_int, here("data","dta_grp_int.parquet"))

rm(dta_grp_int)

#### Over Time Effects ####

dta_grp$yrmonth = format(as.Date(dta_grp$demog_date_time), "%Y-%m")
yrmonths = sort(unique(dta_grp$yrmonth))[1:17]

effects_ot = map_dfr(yrmonths, \(x){
  print(x)
  dta_sub = dta_grp |> 
    filter(yrmonth == x)
  table_conjoint_new(conjoint = dta_sub,
                     lab = policies,
                     weights = dta_sub$demog_weight,
                     use="both",
                     grouped_data=FALSE) |> 
    mutate(yrmonth = x)
})

export(effects_ot, here("data", "effects_ot.rds"))

#### Raw Differences ####

survey_policies = paste0("policy_",policy_list)

dta_grp_recoded = dta_grp |> 
  mutate(across(starts_with("policy"), ~ case_when(.x == 1 ~ 1,
                                                   .x == 2 ~ 0,
                                                   .default = NA))) |> 
  group_by(person_id, wave) |> 
  slice_head(n = 1) |> 
  ungroup() |> 
  srvyr::as_survey_design(weights = demog_weight)

# Calculate raw differences
agree_diffs = map_dfr(survey_policies, \(x){
  bind_rows(
    inc_mod = survey::svyglm(paste0(x, " ~ inc_grp"), design = dta_grp_recoded) |> broom::tidy(),
    edu_mod = survey::svyglm(paste0(x, " ~ edu_grp"), design = dta_grp_recoded) |> broom::tidy()
  ) |> 
    mutate(policy = x)
})

export(agree_diffs, here("data","agree_diffs.rds"))

rm(dta_grp_recoded)